/* 
 * Copyright (c) 2004-2005 Massachusetts Institute of Technology. This code was
 * developed as part of the Haystack (http://haystack.lcs.mit.edu/) research 
 * project at MIT. Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including without 
 * limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions: 
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. 
 */
/*
 * Created on Jul 17, 2004
 *  
 */
package edu.mit.csail.relo.modelBridge;

import org.apache.log4j.Logger;
import org.openrdf.model.URI;

import edu.mit.csail.relo.ReloPlugin;
import edu.mit.csail.relo.parts.ArtifactEditPart;
import edu.mit.csail.relo.utils.DbgRes;

/**
 * There represent Relationship instances
 * 
 * Property relationRes should correspond to RelType 
 * 
 * @author vineet
 *
 */
public class ArtifactRel {
	// below only stored for debugging purposes
    static final Logger logger = ReloPlugin.getLogger(ArtifactRel.class);
	public DbgRes tag = new DbgRes(ArtifactRel.class, this);
	private ArtifactEditPart srcAEP;
	private ArtifactEditPart dstAEP;
	
	protected Artifact srcArt;
	protected Artifact dstArt;

	private URI relationRes;
	
	public ArtifactRel(Artifact srcArt, Artifact dstArt, URI relationRes) {
        init(srcArt, dstArt, relationRes);
	}
	
    public ArtifactRel(ArtifactEditPart srcAEP, ArtifactEditPart dstAEP, URI k) {
        init(srcAEP, dstAEP, k);
    }
    
    public void init(Artifact srcArt, Artifact dstArt, URI relationRes) {
        if (relationRes == null) throw new IllegalArgumentException();
        
        this.srcArt = srcArt;
        this.dstArt = dstArt;
        
        this.relationRes = relationRes;
    }

    public void init(ArtifactEditPart srcAEP, ArtifactEditPart dstAEP, URI relationRes) {
        if (relationRes == null) throw new IllegalArgumentException();
        
        this.srcAEP = srcAEP;
        this.dstAEP = dstAEP;
        init(srcAEP.getArtifact(), dstAEP.getArtifact(), relationRes);
    }

    public Artifact getSrc() {
	    return srcArt;
	}
	public Artifact getDest() {
	    return dstArt;
	}
    public URI getType() {
        return relationRes;
    }

	@Override
    public String toString() {
		if (srcAEP  != null)
			return  srcAEP + " --> " + dstAEP + tag.getTrailer();
		else
			return  srcArt + " --> " + dstArt;
	}
	
	/*
	 * Define hashcode such that it is unique for every src,dst,relType combo! 
	 */
    @Override
    public int hashCode() {
        return 3*srcArt.hashCode() + 2*getType().hashCode() + dstArt.hashCode();
    }

    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object o) {
        if (!(o instanceof ArtifactRel)) return false;
        ArtifactRel ar = (ArtifactRel) o; 
        if (ar.srcArt.equals(srcArt) && ar.getType().equals(getType()) && ar.dstArt.equals(dstArt)) return true;
        return false;
    }
}